﻿using System;
using Super_Management_Mayhem.Content.Spritesheet.Player;
using Transformable_Engine_v2.Engine.Animation.ColorAnimation;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Players.Manager
{
    public partial class Manager
    {
        private TE_Vector2 _hitDirection;
        private int _hurtTime = 500;
        private const int InvincibilityTimer = 1500;
        private Timer _hurtTimer;
        private Timer _hurtInvincibilityTimer;
        private bool _isInvincible;

        void InitializeHurtStateObjects()
        {
            _isInvincible = false;
            _hitDirection = TE_Vector2.Zero;
            _hurtTimer = new Timer(1, false, EndHurt);
            _hurtInvincibilityTimer = new Timer(InvincibilityTimer, false, EndInvincibility);

            AddTimers(_hurtTimer, _hurtInvincibilityTimer);
        }

        private void EndInvincibility()
        {
            _isInvincible = false;
            _character.ClearAnimation<ColorAnimation>();
            _character.Color = Color.White;
        }

        private void EndHurt()
        {
            if(_currentPlayerState != PlayerState.Fall)
            {
                _player.ClearAnimation<PositionUniformLinearAnimation>();
                ChangeState(PlayerState.Idle);
            }
        }

        public bool Hit(TE_Vector2 direction, TE_Vector2 attackerPosition, int hurtTime, int damage)
        {
            if(_currentPlayerState != PlayerState.Hurt &&
                _currentPlayerState != PlayerState.Dead &&
                _currentPlayerState != PlayerState.Creation &&
                _currentPlayerState != PlayerState.Fall &&
                !_isInvincible) 
            {
                _hitDirection = new TE_Vector2(direction);
                _isInvincible = true;
                _hurtTime = hurtTime;

                _latestDirection = attackerPosition.X < _player.Position.X ? Direction.Left : Direction.Right;

                _statistics.LosePoints(damage);

                ChangeState(PlayerState.Hurt);
                return true;
            }

            return false;
        }

        void InitializeHurtState()
        {
            if(_isCarrying)
                DestroyCarriedObject();

            if(!_statistics.Alive)
            {
                ChangeState(PlayerState.Dead);
                return;
            }

            switch(_latestDirection) 
            {
                case Direction.Left:
                    _character.Sprite = ManagerSheet.GetSprite(ManagerSheet.HitLeftState.Hurt);
                    _character.OriginAbsolute = new TE_Vector2(8, 26);
                    break;
                case Direction.Right:
                    _character.Sprite = ManagerSheet.GetSprite(ManagerSheet.HitRightState.Hurt);
                    _character.OriginAbsolute = new TE_Vector2(23, 26);
                    break;
            }

            _character.Color = Color.White;
            _character.AddAnimations(new ColorAnimation(new SinusTrajectory(1), Color.Transparent, 100, true, 0));
            _player.AddAnimations(new PositionUniformLinearAnimation(0, _hitDirection, -_hitDirection * (1000.0f / _hurtTime)));

            _hurtTimer.Reset(_hurtTime);
            _hurtInvincibilityTimer.Reset();
        }

        void HandleKeyPressHurtState(Input input)
        {
        }

        void HandleKeyReleaseHurtState(Input input)
        {

        }

        void UpdateHurtState(TimeSpan gameTime)
        {
            HandleMovement(_hitDirection, false);
        }
    }
}